<?php
  require(dirname(__FILE__)."/global.php");
  require(ROOT."/admin/isLogin.php");
                                                 
  $submit = $_POST['submit'];
  if(isset($submit))
  {
      $dels = $_POST['del'];
      $orderNos = $_POST['orderNo'];
      $names = $_POST['name'];      
      $cates = array();  
      
      for($i = 0; $i < count($dels); $i++){
          $sql = "delete from talking_cate where CateID=".$dels[$i];
          $db->execute($sql);
      }
      
      for($i = 0; $i < count($names); $i++){
          $temp = array();
          if(empty($names[$i])) continue;
          $temp['orderNo'] = empty($orderNos[$i]) ? 0 : $orderNos[$i];
          $temp['name'] = $names[$i];
          $temp['manager'] = $_SESSION['user']['id'];
          $cates[] = $temp;
      }
      
      foreach($cates as $cate){
          $sql = "insert into talking_cate(CateName,CateOrderNo,CateLevel,CateVisible,CateManager) values('".$cate['name']."',".$cate['orderNo'].",1,1,'".$cate['manager']."')";           
          $db->execute($sql);
      }
      $smarty->assign('tip','提交成功');
  }
  $sql = "SELECT tc.*,tu.UserName FROM talking_cate tc,talking_user tu WHERE tc.CateManager = tu.UserID ORDER BY tc.CateOrderNo";
  $cates = $db->query($sql);
  $smarty->assign("cates",$cates);
  $smarty->display("cate.html");
  $db->close();                           
?>
